﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;
using ECare.Database;
using ECare.BUS.Data;


namespace ECare.BUS
{
    public class ResourceOutHelper
    {

        public static int CreateResourceOutInfo(ResourceOutInfo outinfo)
        {
            using (var context = new Entities())
            {
                var newout = new resource_out();
                newout.resource_sheet_no = outinfo.resource_out_sheet_no;
                newout.resource_id = outinfo.resource_id;
                newout.resource_numbers = outinfo.resource_numbers;
                newout.resource_get_by = outinfo.resource_get_by;
                newout.resource_get_time = outinfo.resource_get_time;
                newout.resource_remark = outinfo.resource_remark;


                newout.resource_create_by = ApplicationData.CurrentUserInfo.UserId;
                newout.resource_create_time = DateTime.Now;
                



                context.resource_out.AddObject(newout);
                context.SaveChanges();
                outinfo.resource_out_id = newout.resource_out_id;
                return outinfo.resource_out_id;
            }
        }


        public static bool UpdateResourceOutInfo(ResourceOutInfo outinfo)
        {
            using (var context = new Entities())
            {
                var findinfo = context.resource_out.FirstOrDefault(a => a.resource_out_id == outinfo.resource_out_id);
                if (findinfo == null)
                    return false;

                findinfo.resource_id = outinfo.resource_id;
                findinfo.resource_numbers = outinfo.resource_numbers;
                findinfo.resource_get_by = outinfo.resource_get_by;
                findinfo.resource_get_time = outinfo.resource_get_time;
                findinfo.resource_remark = outinfo.resource_remark;

                context.SaveChanges();
                return true;
            }
        }


        public static DataTable GetOutDetails(int outId)
        {
            using (var context = new Entities())
            {
                var resources = context.sp_getoutdetails(outId).ToList();

                DataTable res = new DataTable();
                res.Columns.Add("code");
                res.Columns.Add("numbers");


                foreach (var resource in resources)
                {
                    var newrow = res.NewRow();
                    newrow["code"] = resource.supply_code;
                    newrow["numbers"] = resource.numbers;
                    res.Rows.Add(newrow);
                }

                return res;

            }
        }


        public static List<ResourceOutInfo> GetOutINfos(string where="1=1")
        {
            using (var context = new Entities())
            {
                var outs = context.sp_outquery(where).ToList();

                var res = new List<ResourceOutInfo>();

                foreach (var outinfo in outs)
                {
                    var newout = new ResourceOutInfo
                    {
                        resource_create_by = outinfo.resource_create_by,
                        resource_create_time = outinfo.resource_create_time,
                        resource_get_by = outinfo.resource_get_by.Value,
                        resource_get_time = outinfo.resource_get_time,
                        resource_id = outinfo.resource_id,
                        resource_numbers = outinfo.resource_numbers,
                        resource_out_id = outinfo.resource_out_id,
                        resource_out_sheet_no = outinfo.resource_sheet_no,
                        resource_remark = outinfo.resource_remark,
                        resource_status = outinfo.resource_status.HasValue ? (CheckStatus)outinfo.resource_status.Value : CheckStatus.Pending,
                     
                    };

                    res.Add(newout);
                }
                return res;
            }

        }


    }
}
